﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WomanMotion.WebApi.Infrastructure.Common;
using WomanMotion.WebApi.Infrastructure.DataBaseCommon;
using WomanMotion.WebApi.Model.Dtos.Classify;
using WomanMotion.WebApi.Model.Dtos.Match;
using WomanMotion.WebApi.Model.Dtos.Schedule;
using WomanMotion.WebApi.Model.Dtos.Team;
using WomanMotion.WebApi.Model.Dtos.Users;
using WomanMotion.WebApi.Model.Dtos.Watch;
using WomanMotion.WebApi.Model.Entitys;
using WomanMotion.WebApi.Repository.IRepository;
using WomanMotion.WebApi.Service.IService;

namespace WomanMotion.WebApi.Service.Service
{
    public class ScheduleService : BaseCRUDService<Schedule, ScheduleVM, UpdateScheduleDTO, CreatedScheduleDTO>, IScheduleService
    {
        private readonly IScheduleRepository _scheduleRepository;
        private readonly IMatchService _matchService;

        public ScheduleService(ILogger<ScheduleService> logger, IMapper mapper, IScheduleRepository scheduleRepository, IHttpContextAccessor httpContext, IMatchService matchService) : base(logger, mapper, scheduleRepository, httpContext)
        {
            _scheduleRepository = scheduleRepository;
            _matchService = matchService;
        }

        ///// <summary>
        ///// 分页获取赛程管理比赛的数据
        ///// </summary>
        ///// <param name="pars">查询参数</param>
        ///// <returns></returns>
        public async Task<PageViewBase<List<ScheduleAndMatchPageVM>>> GetScheduleAndWatchPageListAsync(QueryParameterBase pars)
        {
            //    //首先查询分页的赛程数据
            var schedule = await this.GetPageNavListAsync(pars);
            var res = new PageViewBase<List<ScheduleAndMatchPageVM>>();
            res.PageNum = schedule.PageNum;
            res.PageSize = schedule.PageSize;
            res.Total = schedule.Total;
            //定义变量存储 List<ScheduleAndMatchPageVM>
            var listsampv = new List<ScheduleAndMatchPageVM>();
            //循环查询到的赛程数据
            foreach (var item in schedule.Data)
            {
                var sampv = new ScheduleAndMatchPageVM();
                sampv.datatype = "schedule";
                sampv.idstr = "schedule," + item.Id.ToString();
                sampv.Id = item.Id;
                sampv.Classid = item.Classid;
                sampv.Classify = item.Classify;
                sampv.Scheduleid = item.Id;
                sampv.Schedule = item;
                sampv.Starttime = item.Starttime;
                sampv.StarttimeHour = null;
                sampv.StarttimeWeek = DateTimeTool.Week(item.Starttime);
                sampv.Teamaid = 0;
                sampv.Teama = null;
                sampv.Teambid = 0;
                sampv.Teamb = null;
                sampv.Teamascore = 0;
                sampv.Teambscore = 0;
                sampv.Watchid = 0;
                sampv.Watch = null;
                sampv.Remark = item.Remark;
                sampv.Createdby = item.Createdby;
                sampv.Createdby = item.Createdby;
                sampv.Createdtime = item.Createdtime;
                sampv.Updatedby = item.Updatedby;
                sampv.Updatedby = item.Updatedby;
                sampv.Updatedtime = item.Updatedtime;
                sampv.Ifdisable = item.Ifdisable;
                //获取赛程下面的比赛信息
                var listmatch = await _matchService.GetListNavOrderByAsync(m => m.Scheduleid == item.Id, new string[1] { "id desc" });
                //定义变量存储比赛集合
                var listsampbv = new List<ScheduleAndMatchPageBaseVM>();
                //循环比赛信息
                foreach (var itemmatch in listmatch)
                {
                    var match = new ScheduleAndMatchPageBaseVM();
                    match.datatype = "match";
                    match.idstr = "match," + itemmatch.Id.ToString();
                    match.Id = itemmatch.Id;
                    match.Classid = itemmatch.Classid;
                    match.Classify = itemmatch.Classify;
                    match.Scheduleid = itemmatch.Scheduleid;
                    match.Schedule = itemmatch.Schedule;
                    match.Starttime = itemmatch.Starttime;
                    match.StarttimeHour = itemmatch.StarttimeHour;
                    match.StarttimeWeek = itemmatch.StarttimeWeek;
                    match.Teamaid = itemmatch.Teamaid;
                    match.Teama = itemmatch.Teama;
                    match.Teambid = itemmatch.Teambid;
                    match.Teamb = itemmatch.Teamb;
                    match.Teamascore = itemmatch.Teamascore;
                    match.Teambscore = itemmatch.Teambscore;
                    match.Watchid = itemmatch.Watchid;
                    match.Watch = itemmatch.Watch;
                    match.Remark = itemmatch.Remark;
                    match.Createdby = itemmatch.Createdby;
                    match.Createdby = itemmatch.Createdby;
                    match.Createdtime = itemmatch.Createdtime;
                    match.Updatedby = itemmatch.Updatedby;
                    match.Updatedby = itemmatch.Updatedby;
                    match.Updatedtime = itemmatch.Updatedtime;
                    match.Ifdisable = itemmatch.Ifdisable;
                    listsampbv.Add(match);
                }
                sampv.children = listsampbv;
                //赛程关联比赛信息添加到集合
                listsampv.Add(sampv);
            }
            res.Data = listsampv;
            return res;
        }
    }
}